<%--***************************************
*
*财付通
* 
***************************************--%>
<%@page pageEncoding="UTF-8" %>
<%@ page import="
	java.util.*,
	java.math.*,
	java.text.*,
	com.probiz.estore.payment.tenpay.PayRequestHandler,
	com.probiz.estore.payment.tenpay.util.TenpayUtil,
	com.probiz.estore.common.model.order.SalesOrder,
	com.probiz.estore.common.model.system.PaymentGateway,
	com.probiz.estore.common.helper.ConfigUtil"
	contentType="text/html; charset=UTF-8"%>

<%
	SalesOrder salesOrder = (SalesOrder)request.getAttribute("salesOrder");
	PaymentGateway paymentGateway = (PaymentGateway)request.getAttribute("paymentGateway");
	request.setAttribute("paymentGateway",paymentGateway);
	HashMap paramMap = paymentGateway.getConfigData();
	
	//-----------------------start: 账户信息------------------------------------------
	String key = "";			//平台商密钥
	String bargainor_id = "";	//平台商帐号
	if (paramMap != null) {
		//平台商密钥
		key = paramMap.get("key").toString().trim();
		//平台商帐号
		bargainor_id = paramMap.get("partnerId").toString().trim();
	}
	//-----------------------end:	账户信息------------------------------------------
	
	//-----------------------start: 配置信息------------------------------------------
	String return_url = ConfigUtil.getInstance().getStoreFrontSiteUrl() + "/system/payment/tenpay_response.html";  	//回调通知URL
	
	//-----------------------end:   配置信息------------------------------------------
	
	//-----------------------start: 业务信息------------------------------------------	
	//当前时间 yyyyMMddHHmmss
	String currTime = TenpayUtil.getCurrTime();
	
	//8位日期
	String strDate = currTime.substring(0, 8);
	
	//6位时间
	String strTime = currTime.substring(8, currTime.length());
	
	//四位随机数
	String strRandom = TenpayUtil.buildRandom(4) + "";
	
	//10位序列号,可以自行调整。
	String strReq = strTime + strRandom;
	
	//商家订单号,长度若超过32位，取前32位。财付通只记录商家订单号，不保证唯一。
	String sp_billno = salesOrder.getOrderNo();
	
	//财付通交易单号，规则为：10位商户号+8位时间（YYYYmmdd)+10位流水号
	String transaction_id = bargainor_id + strDate + strReq;
	
	//交易金额以分为单位，所以先乘以100，从0.01变成1
	BigDecimal price_q=salesOrder.getUnPaidAmount().multiply(new BigDecimal(100));
	String total_fee =new DecimalFormat("0").format(price_q.doubleValue());
	
	
	//创建PayRequestHandler实例
	PayRequestHandler reqHandler = new PayRequestHandler(request, response);
	
	//设置密钥
	reqHandler.setKey(key);
	
	//初始化
	reqHandler.init();
	
	//-----------------------------
	//设置支付参数
	//-----------------------------
	reqHandler.setParameter("bargainor_id", bargainor_id);						//商户号
	reqHandler.setParameter("sp_billno", sp_billno);							//商家订单号
	reqHandler.setParameter("transaction_id", transaction_id);					//财付通交易单号
	reqHandler.setParameter("return_url", return_url);							//支付通知url
	reqHandler.setParameter("desc", "订单：" + sp_billno);						//商品名称
	reqHandler.setParameter("total_fee", total_fee);							//商品金额,以分为单位
	reqHandler.setParameter("cs", "utf-8");										//字符编码标准，gbk或者utf-8
	reqHandler.setParameter("spbill_create_ip", request.getRemoteAddr());  		//用户ip,测试环境时不要加这个ip参数，正式环境再加此参数
	
	//获取请求带参数的url
	String requestUrl = reqHandler.getRequestURL();
	
	//获取debug信息
	String debuginfo = reqHandler.getDebugInfo();
	
	System.out.println("requestUrl:" + requestUrl);
	System.out.println("debuginfo:" + debuginfo);
	//-----------------------end:	 业务信息------------------------------------------	
%>

<body onload="location.href='<%=requestUrl%>'">
	<%@ include file="./include/payOrderTips.jspf"%>
</body>
